## Chapter 4 (Issue Positions) 
## Spring 2022 

##################################
## Figure 4.1: Polarized Issues ##
##################################

## as a faceted plot 
pdf(file="Figures/Druckman_etal_figure_4_1.pdf",
    height = 8.5,
    width = 11)
print(data %>%
        filter(year == 4) %>% 
        select(c(minwageopp2, acaoppose2, undocumentedoppose2,apstable,dem)) %>% 
        pivot_longer(cols = c(minwageopp2, acaoppose2, undocumentedoppose2),
                     names_to = "group",
                     values_to = "eval") %>% 
        ## remove people who don't answer the policy questions  
        filter(!is.na(eval)) %>%   
        ## make a variable labeling the norms
         mutate(policy_factor = factor(group,
                                     levels = c("acaoppose2","minwageopp2","undocumentedoppose2"),
                                     labels = c("Affordable Care Act",
                                                "$15 Minimum Wage",
                                                "Pathway to Citizenship for the Undocumented"))) %>% 
        ggplot(aes(apstable, eval, 
                   group = dem)) + 
        geom_smooth(aes(color = as.factor(dem),
                        fill = as.factor(dem)),
                    method = mgcv::gam,
                    se = T) + 
        scale_color_manual(values = c("grey70","black"), 
                           labels = c("Republicans","Democrats")) + 
        scale_fill_manual(values = c("grey70","black"), 
                          labels = c("Republicans","Democrats")) +  
        scale_x_continuous(breaks = ap_quantiles, 
                           labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
        scale_y_continuous(breaks = c(1:4),
                           limits = c(1,4),
                           labels = c("Strongly \n Oppose","Somewhat \n Oppose",
                                      "Somewhat \n Support", "Strongly \n Support")) + 
        xlab("Animus")+
        ylab("Policy Position") + 
        #ylim(c(1,4)) + 
        #ggtitle("Cue-Taking When Both Parties Send Clear Cues") + 
        labs(color = "") + 
        theme_bw() + 
        facet_grid(cols = vars(policy_factor)) + 
        theme(plot.title = element_text(hjust=0.5),
              legend.position = "none", 
              panel.grid = element_blank()))   
dev.off() 

##########################################
## Figure 4.3: Only One Party Polarized ##
########################################## 

## as a faceted plot 
pdf(file="Figures/Druckman_etal_figure_4_3.pdf",
    height = 8.5,
    width = 11)
print(data %>%
        filter(year == 4) %>% 
        select(c(gunopposeban2, familyleaveoppose2, greenndoppose2, defundpoloppose2, apstable,dem)) %>% 
        pivot_longer(cols = c(gunopposeban2, familyleaveoppose2, greenndoppose2, defundpoloppose2),
                     names_to = "group",
                     values_to = "eval") %>% 
        ## remove people who don't answer the policy questions  
        filter(!is.na(eval)) %>%   
        ## make a variable labeling the norms
        mutate(policy_factor = factor(group,
                                      levels = c("gunopposeban2","familyleaveoppose2","defundpoloppose2","greenndoppose2"),
                                      labels = c("Assault Weapons Ban",
                                                 "Paid Family Leave",
                                                 "Defund the Police",
                                                 "Green New Deal"))) %>% 
        ggplot(aes(apstable, eval, 
                   group = dem)) + 
        geom_smooth(aes(color = as.factor(dem),
                        fill = as.factor(dem)),
                    se = T) + 
        scale_color_manual(values = c("grey70","black"), 
                           labels = c("Republicans","Democrats")) + 
        scale_fill_manual(values = c("grey70","black"), 
                          labels = c("Republicans","Democrats")) +  
        scale_x_continuous(breaks = ap_quantiles, 
                           labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
        scale_y_continuous(breaks = c(1:4),
                           limits = c(1,4),
                           labels = c("Strongly \n Oppose","Somewhat \n Oppose",
                                      "Somewhat \n Support", "Strongly \n Support")) + 
        xlab("Animus")+
        ylab("Policy Position") + 
        #ylim(c(1,4)) + 
        #ggtitle("Cue-Taking When Only One Parties Sends a Clear Cue") + 
        labs(color = "") + 
        theme_bw() + 
        facet_grid(cols = vars(policy_factor)) + 
        theme(plot.title = element_text(hjust=0.5),
              legend.position = "none", 
              panel.grid = element_blank()))   
dev.off() 


######################################
## Figure 4.2: Not Polarized Issues ## 
######################################

## as a faceted plot 
pdf(file="Figures/Druckman_etal_figure_4_2.pdf",
    height = 8.5,
    width = 11)
print(data %>%
        filter(year == 4) %>% 
        select(c(lobbyopposeban2, donationsoppose2, immigtestoppos2, relocationoppose2, lowerdrugoppose2, apstable,dem)) %>% 
        pivot_longer(cols = c(lobbyopposeban2, donationsoppose2, immigtestoppos2, relocationoppose2, lowerdrugoppose2),
                     names_to = "group",
                     values_to = "eval") %>% 
        ## remove people who don't answer the policy questions  
        filter(!is.na(eval)) %>%   
        ## make a variable labeling the norms
        mutate(policy_factor = factor(group,
                                      levels = c("donationsoppose2","relocationoppose2","lobbyopposeban2","lowerdrugoppose2","immigtestoppos2"),
                                      labels = c("Allow Unlimited \n Campaign Donations",
                                                 "Public Relocation \n Incentives",
                                                 "Ban on Officials \n Becoming Lobbyists",
                                                 "Negotiate Medicare \n Drug Prices",
                                                 "Citizenship Test for \n Immigrants"))) %>% 
        ggplot(aes(apstable, eval, 
                   group = dem)) + 
        geom_smooth(aes(color = as.factor(dem),
                        fill = as.factor(dem)),
                    se = T) + 
        scale_color_manual(values = c("grey70","black"), 
                           labels = c("Republicans","Democrats")) + 
        scale_fill_manual(values = c("grey70","black"), 
                          labels = c("Republicans","Democrats")) +  
        scale_x_continuous(breaks = ap_quantiles_small, 
                           labels = c("5th \n pct","33rd \n pct","67th \n pct","95th \n pct")) + 
        scale_y_continuous(breaks = c(1:4),
                           limits = c(1,4),
                           labels = c("Strongly \n Oppose","Somewhat \n Oppose",
                                      "Somewhat \n Support", "Strongly \n Support")) + 
        xlab("Animus")+
        ylab("Policy Position") + 
        #ylim(c(1,4)) + 
        #ggtitle("Cue-Taking When Neither Party Sends Clear Cues") + 
        labs(color = "") + 
        theme_bw() + 
        facet_grid(cols = vars(policy_factor)) + 
        theme(plot.title = element_text(hjust=0.5),
              legend.position = "none", 
              panel.grid = element_blank()))   
dev.off() 


#######################
## Regression Models ##
####################### 

# Creating other variables we should control for: 1) education; 2) religion as fixed effect
data <- data %>%
  # Political knowledge scale from 0 to 5 based on number of Qs answered correctly
  mutate(p_knowledge_veto = ifelse(is.na(p_knowledge_veto) == TRUE, 0, 
                                   p_knowledge_veto),
         p_knowledge_hofr = ifelse(is.na(p_knowledge_hofr) == TRUE, 0, 
                                   p_knowledge_hofr),
         p_knowledge_constitution = ifelse(is.na(p_knowledge_constitution) == TRUE, 
                                           0, p_knowledge_constitution),
         p_knowledge_vp = ifelse(is.na(p_knowledge_vp) == TRUE, 0,
                                 p_knowledge_vp),
         p_knowledge_conservative = ifelse(is.na(p_knowledge_conservative) == TRUE,
                                           0, p_knowledge_conservative),
         know1 = ifelse(p_knowledge_veto == 4, 1, 0),
         know2 = ifelse(p_knowledge_hofr == 1, 1, 0),
         know3 = ifelse(p_knowledge_constitution == 3, 1, 0),
         know4 = ifelse(p_knowledge_vp == 3, 1, 0),
         know5 = ifelse(p_knowledge_conservative == 2, 1, 0),
         knowledge = know1 + know2 + know3 + know4 + know5,
         
         # Indicator variables for religious groups
         protestant = if_else(religion == 1,1,0),
         catholic = if_else(religion == 2,1,0),
         jewish  = if_else(religion == 3,1,0),
         other = if_else(religion == 4 | religion == 5 | religion == 6,1,0),
         not_relig = if_else(religion == 7,1,0),
         
         # Indicator variable for non-Hispanic white
         white = ifelse(race == 1, 1, 0),
         
         # Indicator variable for female
         female = ifelse(gender == 2, 1, 
                         ifelse(gender == 1 |
                                  gender == 3 |
                                  gender == 4, 0, NA)))  


## partisan social identity 
## calculate strong partisan, strong ideologue, sorted 
data <- data %>% 
  rowwise() %>% 
  mutate(sociden_pid = mean(c(party_importance,party_describe,party_wethey,party_extent),
                            na.rm=T)) %>% 
  ungroup() 

## Regression models in the appendix 
## both sides polarized 
m1 <- lm(minwageopp2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
         data = data, 
         subset = year == 4) 
m2 <- lm(acaoppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
        data = data, 
        subset = year == 4) 
m3 <- lm(undocumentedoppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
        data = data, 
        subset = year == 4) 
## one side polarized 
m4 <- lm(gunopposeban2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
         data = data, 
         subset = year == 4) 
m5 <- lm(familyleaveoppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid,  
         data = data, 
         subset = year == 4) 
m6 <- lm(greenndoppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
         data = data, 
         subset = year == 4) 
m7 <- lm(defundpoloppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
         data = data, 
         subset = year == 4) 
## neither side polarized 
m8 <- lm(lobbyopposeban2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
         data = data, 
         subset = year == 4) 
m9 <- lm(donationsoppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
           white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
         data = data, 
         subset = year == 4) 
m10 <- lm(immigtestoppos2 ~ apstable*rep + knowledge + wave1ideology + education + 
            white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
         data = data, 
         subset = year == 4) 
m11 <- lm(relocationoppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
            white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid,  
          data = data, 
          subset = year == 4) 
m12 <- lm(lowerdrugoppose2 ~ apstable*rep + knowledge + wave1ideology + education + 
            white + female + protestant + catholic + jewish + not_relig + age + tvnews_fox + sociden_pid, 
          data = data, 
          subset = year == 4) 

## Make these into tables 
## Polarized Issues 
models <- list()
models[["Minimum Wage"]] <- m1  
models[["Obamacare"]] <- m2 
models[["Pathway to \n Citizenship"]] <- m3

## add the N 
rows <- tribble(~term, ~"Minimum Wage",~"Obamacare",~"Pathway to \n Citizenship", 
                'N',length(m1$residuals),length(m2$residuals),length(m3$residuals))
attr(rows,'position') <- 33 

## clean up the GOF output (just N & R-Squared)
gm <- modelsummary::gof_map 
gm$omit <- TRUE 
gm$omit[5] <- FALSE 

msummary(models,
         add_rows = rows, 
         coef_map = c("(Intercept)" = "Constant",
                      "apstable" = "Partisan Animosity",
                      "rep" = "Republican",
                      "knowledge" = "Political Knowledge",
                      "wave1ideology" = "Liberal-Conservative Self ID",
                      "education" = "Education", 
                      "white" = "Non-Hispanic White",
                      "female" = "Female",
                      "protestant" = "Protestant",
                      "catholic" = "Catholic",
                      "jewish" = "Jewish",
                      "not_relig" = "No Religion/Atheist",
                      "age" = "Age",
                      "tvnews_fox" = "Fox News Viewer", 
                      "sociden_pid" = "Partisan Social Identity",
                      "apstable:rep" = "Partisan Animosity*Republican"), 
         gof_map = gm, 
         stars = T, 
         output =  "Tables/raw/table_A4_1.html") 

## Asymmetric Polarization 
models <- list()
models[["Assault Weapon \n Ban"]] <- m4  
models[["Family Leave"]] <- m5 
models[["Green New Deal"]] <- m6 
models[["Defund the \n Police"]] <- m7 
## add the N 
rows <- tribble(~term, ~"Assault Weapon \n Ban",~"Family Leave",~"Green New Deal",~"Defund the \n Police", 
                'N',length(m4$residuals),length(m5$residuals),length(m6$residuals),length(m7$residuals))
attr(rows,'position') <- 33
msummary(models,
         add_rows = rows, 
         coef_map = c("(Intercept)" = "Constant",
                      "apstable" = "Partisan Animosity",
                      "rep" = "Republican",
                      "knowledge" = "Political Knowledge",
                      "wave1ideology" = "Liberal-Conservative Self ID",
                      "education" = "Education", 
                      "white" = "Non-Hispanic White",
                      "female" = "Female",
                      "protestant" = "Protestant",
                      "catholic" = "Catholic",
                      "jewish" = "Jewish",
                      "not_relig" = "No Religion/Atheist",
                      "age" = "Age",
                      "tvnews_fox" = "Fox News Viewer",
                      "sociden_pid" = "Partisan Social Identity",
                      "apstable:rep" = "Partisan Animosity*Republican"), 
         gof_map = gm, 
         stars = T, 
         output =  "Tables/raw/table_A4_3.html") 

## Not Polarized Issues 
models <- list()
models[["Lobbyist Ban"]] <- m8  
models[["Campaign Donations"]] <- m9 
models[["Relocation \n Incentives"]] <- m11 
models[["Drug Negotiation"]] <- m12 
models[["Immigration Test"]] <- m10 
## add the N 
rows <- tribble(~term, ~"Lobbyist Ban",~"Campaign Donations",~"Relocation \n Incentives",~"Drug Negotiation",~"Immigration Test", 
                'N',length(m8$residuals),length(m9$residuals),length(m11$residuals),length(m12$residuals),length(m10$residuals))
attr(rows,'position') <- 33
msummary(models,
         add_rows = rows, 
         coef_map = c("(Intercept)" = "Constant",
                      "apstable" = "Partisan Animosity",
                      "rep" = "Republican",
                      "knowledge" = "Political Knowledge",
                      "wave1ideology" = "Liberal-Conservative Self ID",
                      "education" = "Education", 
                      "white" = "Non-Hispanic White",
                      "female" = "Female",
                      "protestant" = "Protestant",
                      "catholic" = "Catholic",
                      "jewish" = "Jewish",
                      "not_relig" = "No Religion/Atheist",
                      "age" = "Age",
                      "tvnews_fox" = "Fox News Viewer", 
                      "sociden_pid" = "Partisan Social Identity",
                      "apstable:rep" = "Partisan Animosity*Republican"), 
         gof_map = gm, 
         stars = T, 
         output =  "Tables/raw/Table_A4_2.html") 

## sensitivity analysis
robustness_value(m1, covariates = "apstable:rep") 
m1.sens <- sensemakr(m1,
                     treatment = "apstable:rep",
                     benchmark_covariates = "wave1ideology",
                     kd = 1:4)
summary(m1.sens)
pdf(file="Figures/Extra/sensitivty_analysis_chapter_four.pdf")
print(plot(m1.sens))
dev.off()
## basically, we still have a significant effect as long as the unobserved confounder 
## is less than 2x as strong as ideology (the strongest predictor in the model) 

